#!/usr/bin/env python3  
  
from test_framework.test_framework import BitcoinTestFramework  
from test_framework.util import *  
from test_framework.script import *  
from test_framework.p2p import *  
from test_framework.address import *  
from test_framework.qtum import *  
import pprint  
  
pp = pprint.PrettyPrinter()  
  
class QtumEVMPectraTest(BitcoinTestFramework):  
    def add_options(self, parser):  
        self.add_wallet_options(parser)  
  
    def set_test_params(self):  
        self.setup_clean_chain = True  
        self.num_nodes = 1  
        # Fork activates at block height 2399 (set to 2400 to activate at 2399)
        self.extra_args = [['-txindex', '-logevents=1', '-pectraheight=2400']]  
  
    def skip_test_if_missing_module(self):  
        self.skip_if_no_wallet()  
                  
    def run_test(self):  
        # Start by mining to height 2398 (1 block before fork)
        self.generate(self.nodes[0], 2398)  
        self.node = self.nodes[0]  

        # EIP-2537
        """
        pragma solidity ^0.8.0;

        contract PrecompileCheck {
            function verifyCallSuccess(
                address addr,
                bytes calldata data,
                bytes calldata result
            ) public view returns (bool) {
                (bool ok, bytes memory out) = address(addr).staticcall(data);
                require(ok, "Precompile call failed");
                bool ret = ok && isEqual(result, out);
                return ret;
            }
            
            function verifyCallFail(
                address addr,
                bytes calldata data
            ) public view returns (bool) {
                (bool ok, bytes memory out) = address(addr).staticcall(data);
                require(ok, "Precompile call failed");
                bool ret = ok == false && out.length == 0;
                return ret;
            }

            function isEqual(bytes calldata result, bytes memory out
            ) internal pure returns (bool) {
                if (result.length != out.length) {
                    return false;
                }
                for (uint i = 0; i < result.length; i++) {
                    if (result[i] != out[i]) {
                        return false;
                    }
                }
                return true;
            }
        }
        """
          
        # PrecompileCheck contract bytecode  
        precompile_check_bytecode = (  
            "6080604052348015600e575f5ffd5b506106178061001c5f395ff3fe608060405234801561000f575f5ffd5b5060043610610034575f3560e01c8063889f55c914610038578063979c3bb114610068575b5f5ffd5b610052600480360381019061004d91906103c7565b610098565b60405161005f919061043e565b60405180910390f35b610082600480360381019061007d9190610457565b610167565b60405161008f919061043e565b60405180910390f35b5f5f5f8573ffffffffffffffffffffffffffffffffffffffff1685856040516100c2929190610524565b5f60405180830381855afa9150503d805f81146100fa576040519150601f19603f3d011682016040523d82523d5f602084013e6100ff565b606091505b509150915081610144576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161013b90610596565b60405180910390fd5b5f5f151583151514801561015857505f8251145b90508093505050509392505050565b5f5f5f8773ffffffffffffffffffffffffffffffffffffffff168787604051610191929190610524565b5f60405180830381855afa9150503d805f81146101c9576040519150601f19603f3d011682016040523d82523d5f602084013e6101ce565b606091505b509150915081610213576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161020a90610596565b60405180910390fd5b5f8280156102285750610227868684610239565b5b905080935050505095945050505050565b5f8151848490501461024d575f90506102fd565b5f5f90505b848490508110156102f7578281815181106102705761026f6105b4565b5b602001015160f81c60f81b7effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff19168585838181106102b0576102af6105b4565b5b9050013560f81c60f81b7effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916146102ea575f9150506102fd565b8080600101915050610252565b50600190505b9392505050565b5f5ffd5b5f5ffd5b5f73ffffffffffffffffffffffffffffffffffffffff82169050919050565b5f6103358261030c565b9050919050565b6103458161032b565b811461034f575f5ffd5b50565b5f813590506103608161033c565b92915050565b5f5ffd5b5f5ffd5b5f5ffd5b5f5f83601f84011261038757610386610366565b5b8235905067ffffffffffffffff8111156103a4576103a361036a565b5b6020830191508360018202830111156103c0576103bf61036e565b5b9250929050565b5f5f5f604084860312156103de576103dd610304565b5b5f6103eb86828701610352565b935050602084013567ffffffffffffffff81111561040c5761040b610308565b5b61041886828701610372565b92509250509250925092565b5f8115159050919050565b61043881610424565b82525050565b5f6020820190506104515f83018461042f565b92915050565b5f5f5f5f5f606086880312156104705761046f610304565b5b5f61047d88828901610352565b955050602086013567ffffffffffffffff81111561049e5761049d610308565b5b6104aa88828901610372565b9450945050604086013567ffffffffffffffff8111156104cd576104cc610308565b5b6104d988828901610372565b92509250509295509295909350565b5f81905092915050565b828183375f83830152505050565b5f61050b83856104e8565b93506105188385846104f2565b82840190509392505050565b5f610530828486610500565b91508190509392505050565b5f82825260208201905092915050565b7f507265636f6d70696c652063616c6c206661696c6564000000000000000000005f82015250565b5f61058060168361053c565b915061058b8261054c565b602082019050919050565b5f6020820190508181035f8301526105ad81610574565b9050919050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52603260045260245ffdfea2646970667358221220491270ba3ff352458ecf7585d3dcaa046d83b116e3f6938b746b048a6c967cf464736f6c634300081e0033"  
        )  
          
        # Define test cases with their expected results  
        self.test_cases = [  
            {  
                "name": "add_G1",  
                "success_data": "979c3bb1000000000000000000000000000000000000000000000000000000000000000b0000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000017f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb0000000000000000000000000000000008b3f481e3aaa0f1a09e30ed741d8ae4fcf5e095d5d00af600db18cb2c04b3edd03cc744a2888ae40caa232946c5e7e100000000000000000000000000000000112b98340eee2777cc3c14163dea3ec97977ac3dc5c70da32e6e87578f44912e902ccef9efe28d4a78b8999dfbca942600000000000000000000000000000000186b28d92356c4dfec4b5201ad099dbdede3781f8998ddf929b4cd7756192185ca7b8f4ef7088f813270ac3d48868a210000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000a40300ce2dec9888b60690e9a41d3004fda4886854573974fab73b046d3147ba5b7a5bde85279ffede1b45b3918d82d0000000000000000000000000000000006d3d887e9f53b9ec4eb6cedf5607226754b07c01ace7834f57f3e7315faefb739e59018e22c492006190fba4a870025",  
                "fail_data": "889f55c9000000000000000000000000000000000000000000000000000000000000000b000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000ff00000000000000000000000000000017f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb0000000000000000000000000000000008b3f481e3aaa0f1a09e30ed741d8ae4fcf5e095d5d00af600db18cb2c04b3edd03cc744a2888ae40caa232946c5e7e100000000000000000000000000000000112b98340eee2777cc3c14163dea3ec97977ac3dc5c70da32e6e87578f44912e902ccef9efe28d4a78b8999dfbca942600000000000000000000000000000000186b28d92356c4dfec4b5201ad099dbdede3781f8998ddf929b4cd7756192185ca7b8f4ef7088f813270ac3d48868a2100",  
                "after_fork": {  
                    "success_gas": 55871,  
                    "success_output": 1,  
                    "fail_exception": "Revert",  
                    "fail_gas": 39375754  
                },  
                "before_fork": {  
                    "success_gas": 31001,  
                    "success_output": 0,  
                    "fail_gas": 28723,  
                    "fail_output": 0  
                }  
            },  
            {  
                "name": "msm_G1",  
                "success_data": "979c3bb1000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000012000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000017f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb0000000000000000000000000000000008b3f481e3aaa0f1a09e30ed741d8ae4fcf5e095d5d00af600db18cb2c04b3edd03cc744a2888ae40caa232946c5e7e100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000572cbea904d67468808c8eb50a9450c9721db309128012543902d0ac358a62ae28f75bb8f1c7c42c39a8c5529bf0f4e00000000000000000000000000000000166a9d8cabc673a322fda673779d8e3822ba3ecb8670e461f73bb9021d5fd76a4c56d9d4cd16bd1bba86881979749d28",  
                "fail_data": "889f55c9000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000009f00000000000000000000000000000017f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb0000000000000000000000000000000008b3f481e3aaa0f1a09e30ed741d8ae4fcf5e095d5d00af600db18cb2c04b3edd03cc744a2888ae40caa232946c5e7e1000000000000000000000000000000000000000000000000000000000000000200",  
                "after_fork": {  
                    "success_gas": 65978,  
                    "success_output": 1,  
                    "fail_exception": "Revert",  
                    "fail_gas": 39375730  
                },  
                "before_fork": {  
                    "success_gas": 29483,  
                    "success_output": 0,  
                    "fail_gas": 27181,  
                    "fail_output": 0  
                }  
            },  
            {  
                "name": "add_G2",  
                "success_data": "979c3bb1000000000000000000000000000000000000000000000000000000000000000d00000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000280000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000024aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb80000000000000000000000000000000013e02b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7e000000000000000000000000000000000ce5d527727d6e118cc9cdc6da2e351aadfd9baa8cbdd3a76d429a695160d12c923ac9cc3baca289e193548608b82801000000000000000000000000000000000606c4a02ea734cc32acd2b02bc28b99cb3e287e85a763af267492ab572e99ab3f370d275cec1da1aaa9075ff05f79be00000000000000000000000000000000103121a2ceaae586d240843a398967325f8eb5a93e8fea99b62b9f88d8556c80dd726a4b30e84a36eeabaf3592937f2700000000000000000000000000000000086b990f3da2aeac0a36143b7d7c824428215140db1bb859338764cb58458f081d92664f9053b50b3fbd2e4723121b68000000000000000000000000000000000f9e7ba9a86a8f7624aa2b42dcc8772e1af4ae115685e60abc2c9b90242167acef3d0be4050bf935eed7c3b6fc7ba77e000000000000000000000000000000000d22c3652d0dc6f0fc9316e14268477c2049ef772e852108d269d9c38dba1d4802e8dae479818184c08f9a569d8784510000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000b54a8a7b08bd6827ed9a797de216b8c9057b3a9ca93e2f88e7f04f19accc42da90d883632b9ca4dc38d013f71ede4db00000000000000000000000000000000077eba4eecf0bd764dce8ed5f45040dd8f3b3427cb35230509482c14651713282946306247866dfe39a8e33016fcbe520000000000000000000000000000000014e60a76a29ef85cbd69f251b9f29147b67cfe3ed2823d3f9776b3a0efd2731941d47436dc6d2b58d9e65f8438bad073000000000000000000000000000000001586c3c910d95754fef7a732df78e279c3d37431c6a2b77e67a00c7c130a8fcd4d19f159cbeb997a178108fffffcbd20",  
                "fail_data": "889f55c9000000000000000000000000000000000000000000000000000000000000000d000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000001ff000000000000000000000000000000024aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb80000000000000000000000000000000013e02b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7e000000000000000000000000000000000ce5d527727d6e118cc9cdc6da2e351aadfd9baa8cbdd3a76d429a695160d12c923ac9cc3baca289e193548608b82801000000000000000000000000000000000606c4a02ea734cc32acd2b02bc28b99cb3e287e85a763af267492ab572e99ab3f370d275cec1da1aaa9075ff05f79be00000000000000000000000000000000103121a2ceaae586d240843a398967325f8eb5a93e8fea99b62b9f88d8556c80dd726a4b30e84a36eeabaf3592937f2700000000000000000000000000000000086b990f3da2aeac0a36143b7d7c824428215140db1bb859338764cb58458f081d92664f9053b50b3fbd2e4723121b68000000000000000000000000000000000f9e7ba9a86a8f7624aa2b42dcc8772e1af4ae115685e60abc2c9b90242167acef3d0be4050bf935eed7c3b6fc7ba77e000000000000000000000000000000000d22c3652d0dc6f0fc9316e14268477c2049ef772e852108d269d9c38dba1d4802e8dae479818184c08f9a569d87845100",  
                "after_fork": {  
                    "success_gas": 88064,  
                    "success_output": 1,  
                    "fail_exception": "Revert",  
                    "fail_gas": 39375807  
                },  
                "before_fork": {  
                    "success_gas": 36077,  
                    "success_output": 0,  
                    "fail_gas": 32123,  
                    "fail_output": 0  
                }  
            },  
            {  
                "name": "msm_G2",  
                "success_data": "979c3bb1000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000001a0000000000000000000000000000000000000000000000000000000000000012000000000000000000000000000000000024aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb80000000000000000000000000000000013e02b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7e000000000000000000000000000000000ce5d527727d6e118cc9cdc6da2e351aadfd9baa8cbdd3a76d429a695160d12c923ac9cc3baca289e193548608b82801000000000000000000000000000000000606c4a02ea734cc32acd2b02bc28b99cb3e287e85a763af267492ab572e99ab3f370d275cec1da1aaa9075ff05f79be00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000001638533957d540a9d2370f17cc7ed5863bc0b995b8825e0ee1ea1e1e4d00dbae81f14b0bf3611b78c952aacab827a053000000000000000000000000000000000a4edef9c1ed7f729f520e47730a124fd70662a904ba1074728114d1031e1572c6c886f6b57ec72a6178288c47c33577000000000000000000000000000000000468fb440d82b0630aeb8dca2b5256789a66da69bf91009cbfe6bd221e47aa8ae88dece9764bf3bd999d95d71e4c9899000000000000000000000000000000000f6d4552fa65dd2638b361543f887136a43253d9c66c411697003f7a13c308f5422e1aa0a59c8967acdefd8b6e36ccf3",  
                "fail_data": "889f55c9000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000011f000000000000000000000000000000024aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb80000000000000000000000000000000013e02b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7e000000000000000000000000000000000ce5d527727d6e118cc9cdc6da2e351aadfd9baa8cbdd3a76d429a695160d12c923ac9cc3baca289e193548608b82801000000000000000000000000000000000606c4a02ea734cc32acd2b02bc28b99cb3e287e85a763af267492ab572e99ab3f370d275cec1da1aaa9075ff05f79be000000000000000000000000000000000000000000000000000000000000000200",  
                "after_fork": {  
                    "success_gas": 106710,  
                    "success_output": 1,  
                    "fail_exception": "Revert",  
                    "fail_gas": 39375757  
                },  
                "before_fork": {  
                    "success_gas": 32823,  
                    "success_output": 0,  
                    "fail_gas": 28893,  
                    "fail_output": 0  
                }  
            },  
            {  
                "name": "pairing_check",  
                "success_data": "979c3bb1000000000000000000000000000000000000000000000000000000000000000f0000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001800000000000000000000000000000000017f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb0000000000000000000000000000000008b3f481e3aaa0f1a09e30ed741d8ae4fcf5e095d5d00af600db18cb2c04b3edd03cc744a2888ae40caa232946c5e7e10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000001",  
                "fail_data": "889f55c9000000000000000000000000000000000000000000000000000000000000000f000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000002ff00000000000000000000000000000017f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb0000000000000000000000000000000008b3f481e3aaa0f1a09e30ed741d8ae4fcf5e095d5d00af600db18cb2c04b3edd03cc744a2888ae40caa232946c5e7e100000000000000000000000000000000024aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb80000000000000000000000000000000013e02b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7e000000000000000000000000000000000ce5d527727d6e118cc9cdc6da2e351aadfd9baa8cbdd3a76d429a695160d12c923ac9cc3baca289e193548608b82801000000000000000000000000000000000606c4a02ea734cc32acd2b02bc28b99cb3e287e85a763af267492ab572e99ab3f370d275cec1da1aaa9075ff05f79be0000000000000000000000000000000017f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb0000000000000000000000000000000008b3f481e3aaa0f1a09e30ed741d8ae4fcf5e095d5d00af600db18cb2c04b3edd03cc744a2888ae40caa232946c5e7e100000000000000000000000000000000024aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb80000000000000000000000000000000013e02b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7e000000000000000000000000000000000d1b3cc2c7027888be51d9ef691d77bcb679afda66c73f17f9ee3837a55024f78c71363275a75d75d86bab79f74782aa0000000000000000000000000000000013fa4d4a0ad8b1ce186ed5061789213d993923066dddaf1040bc3ff59f825c78df74f2d75467e25e0f55f8a00fa030ed00",  
                "after_fork": {  
                    "success_gas": 103511,  
                    "success_output": 1,  
                    "fail_exception": "Revert",  
                    "fail_gas": 39375860  
                },  
                "before_fork": {  
                    "success_gas": 28885,  
                    "success_output": 0,  
                    "fail_gas": 35476,  
                    "fail_output": 0  
                }  
            },  
            {  
                "name": "map_fp_to_G1",  
                "success_data": "979c3bb10000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000156c8a6a2c184569d69a76be144b5cdc5141d2d2ca4fe341f011e25e3969c55ad9e9b9ce2eb833c81a908e5fa4ac5f03000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000184bb665c37ff561a89ec2122dd343f20e0f4cbcaec84e3c3052ea81d1834e192c426074b02ed3dca4e7676ce4ce48ba0000000000000000000000000000000004407b8d35af4dacc809927071fc0405218f1401a6d15af775810e4e460064bcc9468beeba82fdc751be70476c888bf3",  
                "fail_data": "889f55c900000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000003f00000000000000000000000000000000156c8a6a2c184569d69a76be144b5cdc5141d2d2ca4fe341f011e25e3969c55ad9e9b9ce2eb833c81a908e5fa4ac5f00",  
                "after_fork": {  
                    "success_gas": 58485,  
                    "success_output": 1,  
                    "fail_exception": "Revert",  
                    "fail_gas": 39375718  
                },  
                "before_fork": {  
                    "success_gas": 28481,  
                    "success_output": 0,  
                    "fail_gas": 26191,  
                    "fail_output": 0  
                }  
            },  
            {  
                "name": "map_fp2_to_G2",  
                "success_data": "979c3bb100000000000000000000000000000000000000000000000000000000000000110000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000007355d25caf6e7f2f0cb2812ca0e513bd026ed09dda65b177500fa31714e09ea0ded3a078b526bed3307f804d4b93b040000000000000000000000000000000002829ce3c021339ccb5caf3e187f6370e1e2a311dec9b75363117063ab2015603ff52c3d3b98f19c2f65575e99e8b78c00000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000e7f4568a82b4b7dc1f14c6aaa055edf51502319c723c4dc2688c7fe5944c213f510328082396515734b6612c4e7bb700000000000000000000000000000000126b855e9e69b1f691f816e48ac6977664d24d99f8724868a184186469ddfd4617367e94527d4b74fc86413483afb35b000000000000000000000000000000000caead0fd7b6176c01436833c79d305c78be307da5f6af6c133c47311def6ff1e0babf57a0fb5539fce7ee12407b0a42000000000000000000000000000000001498aadcf7ae2b345243e281ae076df6de84455d766ab6fcdaad71fab60abb2e8b980a440043cd305db09d283c895e3d",  
                "fail_data": "889f55c900000000000000000000000000000000000000000000000000000000000000110000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000007f0000000000000000000000000000000007355d25caf6e7f2f0cb2812ca0e513bd026ed09dda65b177500fa31714e09ea0ded3a078b526bed3307f804d4b93b040000000000000000000000000000000002829ce3c021339ccb5caf3e187f6370e1e2a311dec9b75363117063ab2015603ff52c3d3b98f19c2f65575e99e8b700",  
                "after_fork": {  
                    "success_gas": 106167,  
                    "success_output": 1,  
                    "fail_exception": "Revert",  
                    "fail_gas": 39375728  
                },  
                "before_fork": {  
                    "success_gas": 30965,  
                    "success_output": 0,  
                    "fail_gas": 27023,  
                    "fail_output": 0  
                }  
            }  
        ]  
          
        # Deploy PrecompileCheck contract  
        self.log.info('Deploying PrecompileCheck contract')  
        deploy_result = self.node.createcontract(precompile_check_bytecode)  
        assert 'address' in deploy_result, f"Failed to deploy contract: {deploy_result}"  
        self.contract_address = deploy_result['address']  
        self.sender = deploy_result['sender']  
        self.generate(self.node, 1)  
          
        # Verify we're at height 2399 (1 block before fork activation at 2400)
        current_height = self.node.getblockcount()
        if current_height != 2399:
            raise AssertionError(f"Expected block height 2399, got {current_height}")
          
        # Test all precompiles before fork activation  
        self.log.info("Testing BLS precompiles BEFORE Pectra fork activation (height 2399)")  
        self.test_precompiles(self.test_cases, "before_fork")  
          
        # Mine 1 block to activate the fork (block 2400)  
        self.generate(self.node, 1)  
          
        # Verify we're at height 2400 (fork is now active)
        current_height = self.node.getblockcount()
        if current_height != 2400:
            raise AssertionError(f"Expected block height 2400, got {current_height}")
          
        # Test all precompiles after fork activation  
        self.log.info("Testing BLS precompiles AFTER Pectra fork activation (height 2400)")  
        self.test_precompiles(self.test_cases, "after_fork")  
      
    def test_precompiles(self, test_cases, test_type):  
        """Test all BLS precompiles for the given test type (before_fork or after_fork)"""  
        for test_case in test_cases:  
            self.log.info(f"Testing {test_case['name']} {test_type}")  
              
            # Test success case  
            self.log.info(f"  Testing {test_case['name']} success case")  
            success_info = self.node.callcontract(self.contract_address, test_case["success_data"])  
              
            if test_type == "after_fork":  
                # After fork, success cases should return normally  
                assert_equal(success_info['executionResult']['excepted'], 'None')  
                assert_equal(success_info['executionResult']['gasUsed'], test_case[test_type]['success_gas'])  
                # Convert hex output to integer (remove '0x' prefix first)  
                output_value = int.from_bytes(bytes.fromhex(success_info['executionResult']['output'][2:]), 'big')  
                assert_equal(output_value, test_case[test_type]['success_output'])  
            else:  
                # Before fork, success cases should not actually succeed with 1  
                assert_equal(success_info['executionResult']['excepted'], 'None')  
                assert_equal(success_info['executionResult']['gasUsed'], test_case[test_type]['success_gas'])  
                # Convert hex output to integer (remove '0x' prefix first)  
                output_value = int.from_bytes(bytes.fromhex(success_info['executionResult']['output'][2:]), 'big')  
                assert_equal(output_value, test_case[test_type]['success_output'])  
              
            # Test failure case  
            self.log.info(f"  Testing {test_case['name']} failure case")  
            fail_info = self.node.callcontract(self.contract_address, test_case["fail_data"])  
              
            if test_type == "after_fork":  
                # After fork, failure cases should revert  
                assert_equal(fail_info['executionResult']['excepted'], test_case[test_type]['fail_exception'])  
                assert_equal(fail_info['executionResult']['gasUsed'], test_case[test_type]['fail_gas'])  
            else:  
                # Before fork, failure cases should not revert but return 0  
                assert_equal(fail_info['executionResult']['excepted'], 'None')  
                assert_equal(fail_info['executionResult']['gasUsed'], test_case[test_type]['fail_gas'])  
                # Convert hex output to integer (remove '0x' prefix first)  
                output_value = int.from_bytes(bytes.fromhex(fail_info['executionResult']['output'][2:]), 'big')  
                assert_equal(output_value, test_case[test_type]['fail_output'])  
              
            self.log.info(f"  {test_case['name']} tests passed")  
  
if __name__ == '__main__':  
    QtumEVMPectraTest(__file__).main()